import "package:get/get.dart";
import "package:share_fit/app/controllers/base/base_controller.dart";
import "package:share_fit/app/controllers/device/bind/device_bind_utils.dart";
import "package:share_fit/app/helper/ext/tr_ext.dart";
import "package:share_fit/app/tools/log_utils.dart";
import "package:share_fit/app/ute/uteped_device.dart";
import 'package:share_fit/app/ute/uteped_helper.dart';
import "package:share_fit/app/ute/uteped_const.dart";
import "package:share_fit/generated/locales.g.dart";

import "device_bind_state.dart";

class DeviceBindLogic extends BaseController implements DeviceChangeListener  {
  final DeviceBindState state = DeviceBindState();
  final UTEPedDevice utePedDevice = UTEPedDevice.instance;

  @override
  void onInit() {
    super.onInit();
    UTEPedDevice.instance.addListener(this);
    UTEPedHelper.addListener(this);
  }

  Future<void> connect(String address, String deviceName) async {
    logger.d("connect address: $address");
    DeviceBindUtils.showConnectingDialog(deviceName);
    state.address = address;
    utePedDevice.connect(address);
  }

  void connectStateChanged(int status) {
    logger.d("connectStateChanged...$status");
    state.status = status;
    for (var device in state.deviceList) {
      if (device[UTEPedConst.keyAddress] == state.address) {
        if (state.status == UTEPedConst.stateConnected) {
          update();
          if (state.isConnecting) {
            logger.d("device is connected and dismiss bind dialog");
            DeviceBindUtils.dismissDialog();
            state.isConnecting = false;
            DeviceBindUtils.showConnectedDialog(device[UTEPedConst.keyName]);
            state.isConnected = true;
          }
          else {
            logger.d("device is connected");
          }
        } else if (state.status == UTEPedConst.stateDisconnected) {
          DeviceBindUtils.dismissDialog();
          Get.snackbar(LocaleKeys.info.ts, LocaleKeys.device_connect_failed.ts);
        }
        break;
      }
    }
  }

  @override
  void onRemoteEvent(event, method) {
    if (method == UTEPedConst.methodConnect) {
      connectStateChanged(event[UTEPedConst.keyStatus]);
    }
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    // TODO: implement initState
  }

  @override
  void onClose() {
    UTEPedDevice.instance.removeListener(this);
    if (state.isConnecting || state.isConnected) {
      DeviceBindUtils.dismissDialog();
    }
    state.isConnecting = false;
    state.isConnected = false;
    super.onClose();
  }

  @override
  void onDeviceChangeEvent(event) {
    String address = event[UTEPedConst.keyAddress];
    String action = event[UTEPedConst.keyAction];
    if (action == UTEPedConst.actionConnectTimeout || action == UTEPedConst.actionConnectReject) {
      DeviceBindUtils.dismissDialog();
      // Get.snackbar(LocaleKeys.info.ts, LocaleKeys.device_connect_timeout.ts);
    }
  }
}
